import {Dimensions} from 'react-native';

export default {
    //检查邮箱
    checkEmail(email) {
        return /^([a-zA-Z0-9-_]*[-_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[.][a-zA-Z]{2,3}([.][a-zA-Z]{2})?$/.test(email);
    },
    //检查手机
    checkMobile(mobile) {
        return /^1\d{10}$/.test(mobile);
    },
    //两位长度
    subTime(strTime) {
        if (strTime < 10) {
            strTime = '0' + strTime;
        }
        return strTime + '';
    },
    //货币化
    currency(price, separate = false) {
        let f = parseFloat(price);
        if (isNaN(f)) {
            return 0;
        }
        f = Math.round(f * 100) / 100;
        let s = f.toString();
        let rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        if (separate) {
            return s.split('.');
        }
        return s;
    },
    formatDate(time = Date.now(), fmt = 'yyyy-MM-dd hh:mm:ss') {
        let date = new Date(time);
        let o = {
            'M+': date.getMonth() + 1, //月份
            'd+': date.getDate(), //日
            'h+': date.getHours(), //小时
            'm+': date.getMinutes(), //分
            's+': date.getSeconds(), //秒
            'q+': Math.floor((date.getMonth() + 3) / 3), //季度
            'S': date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        for (let k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
            }
        }
        return fmt;
    },
    //获取中文星期
    getDay(time = Date.now(), prefix = '周') {
        let str = '日一二三四五六';
        return prefix + str[new Date(time).getDay()];
    },
    //获取字符串长度，区分中英文
    len(str = '') {
        let len = 0,
            a = str.split('');
        for (let i = 0; i < a.length; i++) {
            if (a[i].charCodeAt(0) < 299) {
                len++;
            } else {
                len += 2;
            }
        }
        return len;
    },
    //获取uuid
    uuid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },
    //获取[min, max]
    rand(min = 0, max = 100000000) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    },
    //获取屏幕高度和宽度,返回{width,height}
    getScreen() {
        return Dimensions.get('window');
    },
    easeInOutQuad(pos) {
        if ((pos /= 0.5) < 1) {
            return 0.5 * Math.pow(pos, 2);
        }
        return -0.5 * ((pos -= 2) * pos - 2);
    },
    //获取查询参数
    getQuery(name, url) {
        let u = arguments[1],
            reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'),
            r = u.substr(u.indexOf('\?') + 1).match(reg);
        return r !== null ? r[2] : '';
    }
};
